require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var _mm = require('util/mm.js');
var Pagination = require('util/pagination/index.js');
var addressModal = require('./address-modal.js');
var addressTemplate = require('./address.string');
var productTemplate = require('./product.string');
var _product = require('service/product-service.js');
var _address = require('service/address-service.js');
var _order = require('service/order-service.js');

var page = {
  data: {
    selectedAddressId: null
  },
  init: function() {
    this.onLoad();
    this.bindEvent();
  },
  bindEvent: function() {
    _this = this;
    $(document).on('click', '.address-item', function() {
      $(this).addClass('active').siblings('.address-item').removeClass('active');
      _this.data.selectedAddressId = $(this).data('id');
    });
    $(document).on('click', '.order-submit', function() {
      var shippingId = _this.data.selectedAddressId;
      if(shippingId) {
        _order.createOrder({
          shippingId: shippingId
        }, function(res) {
          window.location.href = './payment.html?orderNumber=' + res.orderNo;
        }, function(errMsg) {
          _mm.errorTip(errMsg);
        })
      } else {
        _mm.errorTip('請選擇地址后再提交');
      }
    });
    // 地址的添加
    $(document).on('click', '.address-add', function() {
        addressModal.show({
          isUpdate: false,
          // 回调
          onSuccess: function() {
            _this.loadAddressList();
          }
        });
    });
      // 地址的编辑
    $(document).on('click', '.address-update', function(e) {
        e.stopPropagation();
        var shippingId = $(this).parents('.address-item').data('id');
        _address.getAddress(shippingId, function(res) {
            addressModal.show({
              isUpdate: true,
              data: res,
              // 回调
              onSuccess: function() {
                _this.loadAddressList();
              }
            });
        }, function(errMsg) {
          _mm.errorTip(errMsg);
        });

    });
      // 地址删除
    $(document).on('click', '.address-delete', function(e) {
      e.stopPropagation();
      var shippingId = $(this).parents('.address-item').data('id');
      if(window.confirm('确认要删除该地址')) {
        _address.delAddress(shippingId, function(res) {
          _this.loadAddressList();
        }, function(errMsg) {
          _mm.errorTip(errMsg);
        })
      }
    });
  },
  onLoad: function() {
    this.loadAddressList();
    this.loadProductList();
  },
  loadAddressList: function() {
    var _this = this;
   $('.address-con').html('<div class="loading"></div>');    
    _address.getAddressList(function(res) {
      _this.addressFilter(res);
      var addressHtml = _mm.renderHtml(addressTemplate, res);
      $('.address-con').html(addressHtml);
    }, function(errMsg) {
      $('.address-con').html('<p class="err-tip">地址加载失败，请刷新后重试</p>')
    });
    
  },
  // 处理地址列表中的选中状态
  addressFilter: function(data) {
    if(this.data.selectedAddressId) {
      var selectedAddressIdFlag = false;
      var length = data.list.length;
      for(var i=0; i<length; i++) {
        if(data.list[i].id === this.data.selectedAddressId) {
          data.list[i].isActive = true;
          selectedAddressIdFlag = true;
        }
      }
      // 如果以前选中的地址不在列表里，将其删除
      if(!selectedAddressIdFlag) {
        this.data.selectedAddressId = null;
      }
    }
  },
  loadProductList: function() {
   var _this = this;
   $('.product-con').html('<div class="loading"></div>');
   _order.getProductList(function(res) {
      var productHtml = _mm.renderHtml(productTemplate, res);
      $('.product-con').html(productHtml);
    }, function(errMsg) {
      $('.product-con').html('<p class="err-tip">商品信息加载失败，请刷新后重试</p>')
    });
  }
}
$(function() {
  page.init();
})